<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>购买电影票</title>
    <style>
        body{
           background-color: #3a4459;
        }
        * {
            margin: 0;
            padding: 0;
            text-decoration: none;
            list-style: none;
            background-repeat: no-repeat;

        }

        .con-box {
            width: 100%;
            height: 100vh;
            display: flex;
            justify-content: center;
            gap: 10px;
            margin: 0 auto;
        }

        .left {
            width: 60%;
            height: 100vh;
            border-right: 1px dashed #000;
        }

        .left p {
            width: 70%;
            height: 35px;
            line-height: 35px;
            text-align: center;
            margin: 30px auto;
            background-color: #00aced;
            border-radius: 5px;
        }

        .seat {
            display: flex;
            width: 80%;
            height: 750px;
            flex-wrap: wrap;
            gap: 30px;
            justify-content: space-around;
            align-content: center;
            margin: 0 auto;
        }

        .seat li {
            width: 8%;
            height: 8%;
            border-radius: 5px;
            background-color: #8fd19e;
            line-height: 60px;
            text-align: center;
            cursor: pointer;
        }

        .right {
            width: 35%;
            height: 100vh;
            color: #a79e9f;
            position: relative;
        }

        .right-con {
            width: 350px;
            height: 90vh;
            position: absolute;
            right: 0;
            top: 5%;
            line-height: 28px;
        }
        .right-con li{
           text-decoration-color: #5AB1D0;
        }

        #seatNumbers {
            width: 240px;
        }

        .seat-number {
            display: inline-block;
            width: 100px;
            height: 30px;
            background-color: #cbd3da;
            border-radius: 5px;
            text-align: center;
            margin-left: 10px;
            margin-bottom: 10px;
            line-height: 30px;
            color: #000;
            border: #d1d1d1 1px solid;
        }

        .right-con button {
            width: 70px;
            height: 25px;
            margin: 25px 50px;
            background-color: #efefef;
            border: solid 0.5px #000;
            border-radius: 2px;
            cursor: pointer;
        }


    </style>
</head>
<body>
<div class="con-box">
    <div class="left">
        <p >屏幕</p >
        <ul class="seat"></ul>
    </div>

    <div class="right" >

        <ul class="right-con">
            <li><img src="images/bingxue.jpg" width="250" height="350"></li>
            <li ><span id="name">冰雪奇缘 2</span></li>
            <li>时间：<span id="onlineDate">2024-5-20</span></li>
            <li>观影时间：<span id="onlineTime">12:00-14:00</span></li>
            <li>票数<span id="num"></span></li>
            <li>票价：<span id="price3">50</span>$</li>

            <li>座位：<p id="seatNumbers"></p >
            </li>
            <li>影片类别：<span id="type">梦幻</span></li>
            <li>主演：<span>艾莎.....</span></li>
            <button>确认购买</button>
        </ul>
    </div>
</div>

</body>
<script src="/js/layui.js"></script>
<link rel="stylesheet" href="/css/layui.css"  media="all">

<script>
    document.addEventListener('DOMContentLoaded', function () {
        const seatList = document.querySelector('.seat');

        for (let i = 0; i < 64; i++) {
            const li = document.createElement('li');
            seatList.appendChild(li);
        }
        const seats = document.querySelectorAll('.seat li');
        const defaultColor = seats[0].style.backgroundColor;
        let selectedSeats = [];
        const seatNumbers = document.querySelector('#seatNumbers');

        for (let i = 1; i <= 8; i++) {
            for (let j = 1; j <= 8; j++) {
                let seatIndex = (i - 1) * 8 + (j - 1);
                seats[seatIndex].innerText = i + '-' + j;
            }
        }

        function addSeatClick(seat) {
            seat.addEventListener('click', () => {
                if (seat.style.backgroundColor === defaultColor) {
                    seat.style.backgroundColor = 'red';
                    selectedSeats.push(seat.innerText);

                    let numberP = document.createElement('p');
                    numberP.classList.add('seat-number');
                    seatNumbers.appendChild(numberP);

                    let row = seat.innerText.split('-')[0];
                    let column = seat.innerText.split('-')[1];
                    numberP.innerText = `${row}排${column}座`;

                    document.querySelector('.right-con li:nth-child(5) span').innerText = selectedSeats.length;
                    let total = selectedSeats.length * 50;
                    document.querySelector('.right-con li:nth-child(6) span').innerText = total;
                } else {
                    seat.style.backgroundColor = defaultColor;

                    const seatIndex = selectedSeats.indexOf(seat.innerText);
                    if (seatIndex !== -1) {
                        selectedSeats.splice(seatIndex, 1);
                    }
                    seatNumbers.innerHTML = '';
                    selectedSeats.forEach(seat => {
                        let numberP = document.createElement('p');
                        numberP.classList.add('seat-number');
                        let seatInfo = document.createTextNode(`${seat.split('-')[0]}排${seat.split('-')[1]}座`);
                        numberP.appendChild(seatInfo);
                        seatNumbers.appendChild(numberP);
                    });

                    document.querySelector('.right-con li:nth-child(5) span').innerText = selectedSeats.length;
                    let total = selectedSeats.length * 50;
                    document.querySelector('.right-con li:nth-child(6) span').innerText = '￥' + total;
                }
            });
        }
        function but() {
            const confirmButton = document.querySelector('button');
            confirmButton.addEventListener('click', () => {

                console.log(selectedSeats);
                let userID = null ;
                let movieSeat = JSON.stringify(selectedSeats);
                let movieName = document.getElementById("name").innerText.toString();
                let movieDate = document.getElementById("onlineDate").innerText;
                let movieTime = document.getElementById("onlineTime").innerText;
                let movieNum = document.getElementById("num").innerText;

                let moviePrice = document.getElementById("price3").innerText;
                // var seatNumbers = document.getElementById("seatNumbers").innerText;
                let movieType = document.getElementById("type").innerText.toString();
                let mergeData = {userID,movieName,movieNum,movieDate,moviePrice,movieTime,movieSeat,movieType};
                let json = JSON.stringify(mergeData);
                console.log(json);

                layui.use(['jquery', 'layer'], function(){
                    var $ = layui.$, layer = layui.layer;

                $.ajax({
                    url: 'http://localhost:8081/userorders/add',
                    type: 'post',
                    contentType:"application/json",
                    data:json,
                    success: function (res){
                        layer.alert("购买成功",{icon: 1},function (index){
                            location.reload();
                        });
                    }
                });

                });

                seats.forEach(seat => {
                    seat.style.backgroundColor = '#b9ef9f';
                });
                seatNumbers.innerHTML = '';
                selectedSeats = [];
                document.querySelector('.right-con li:nth-child(5) span').innerText = '';
                document.querySelector('.right-con li:nth-child(6) span').innerText = '';
                // alert('确认购买');
                // // alert('是否确认购买，前往支付？');
                // // 此处添加一个计时器以确保 alert 完成显示后再跳转
                // setTimeout(() => {
                //     window.location.href = "http://localhost:8081/templates/order.html";
                // }, 1000);

            });
        }
        but();
        seats.forEach(seat => {
            addSeatClick(seat);
        });
    });


</script>
</html>
